package com.pr.makfilm.core.cmp.web;

import org.apache.commons.lang3.StringUtils;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.IModel;
import org.apache.wicket.validation.IValidator;

/**
 * Small Panel for TextField with integrated feedback & validation
 */

public class BootstrapTextFieldPanel<T> extends AbstractBootstrapComponent
{
   private static final long serialVersionUID = -7343831494116669887L;

   private TextField<T> textField;

   private String placeholder;

   public BootstrapTextFieldPanel(String id, IModel<T> model)
   {
      super(id);
      textField = new TextField<T>("textField", model);
      container.add(textField);
   }

   public BootstrapTextFieldPanel(String id, IModel<T> model, String cssClazz)
   {
      this(id, model);
      this.cssClass = cssClazz;

   }

   public BootstrapTextFieldPanel(String id, IModel<T> model, String cssClazz, String placeholder)
   {
      this(id, model, cssClazz);
      this.placeholder = placeholder;
   }

   @Override
   protected void onInitialize()
   {
      super.onInitialize();
      if (StringUtils.isNotBlank(placeholder))
      {
         getField().add(new AttributeModifier(placeholder, placeholder));
      }
   }

   @Override
   public void setValidation(boolean required)
   {
      textField.setRequired(required);
   }

   public void addValidator(IValidator validator)
   {
      textField.add(validator);
   }

   public void setInputType(String type)
   {
      textField.add(new AttributeModifier("type", type));
   }

   @Override
   public FormComponent getField()
   {
      return textField;
   }
}
